$(function(){
    populate_cdrs();
    $('nav button').click(populate_sidebar($(this).attr('id')));
    $('#filters').hide();
}); //document.ready() end 

function populate_sidebar(btn_id){
        
        switch (btn_id){
            case "by_ext":
            //populate sidebar with extensions and a search box
                $.getJSON("get_ext.php",function(all_exts){
                    $('#sidebar').empty();
                    //var li;
                    $.each(all_exts, function(i, ext){
                        //li = ;
                        $('#sidebar').append('<li><a href="#">'+ext+'</a></li>');
                    });
                    $('#sidebar').wrapInner('<ul id="ext_list"></ul>');
                    //html += '<ul id="ext_list">'+html+'</ul>';
                    //$('#sidebar').html(html);
                    $('#sidebar a').on('click',function(){
                        $('#sidebar a.active').removeClass('active');
                        $(this).addClass('active');
                        $('#filters').show();
                        populate_cdrs_ext($(this).text());
                    });
                });
                break;
        
            case "by_emp":
            //populate sidebar with empolyee names and a search box
                $.getJSON("get_emps.php", function(all_emps){
                    $('#sidebar').empty();
                    $('#sidebar').addClass('rtl');
                    $.each(all_emps, function(i, emp){
                        $('#sidebar').append('<li><a id="'+emp['id']+'"href="#">'+emp['name']+'</a></li>');
                    });
                    $('#sidebar').wrapInner('<ul id="emp_list"></ul>');
                    $('#sidebar a').on('click',function(){
                        $('#sidebar a.active').removeClass('active');
                        $(this).addClass('active');
                        $('#filters').show();
                        populate_cdrs_emp($(this).attr('id'));
                    });
                });
                break;
        
            case "by_dep":
            //populate sidebar with departments/branches and a search box
                $.getJSON("get_deps.php", function(all_deps){
                    $('#sidebar').empty();
                    $('#sidebar').addClass('rtl');
                    $.each(all_deps, function(i, dep){
                        $('#sidebar').append('<li><a id="'+dep['id']+'"href="#">'+dep['name']+'</a></li>');
                    });
                    $('#sidebar').wrapInner('<ul id="dep_list"></ul>');
                    $('#sidebar a').on('click',function(){
                        $('#sidebar a.active').removeClass('active');
                        $(this).addClass('active');
                        $('#filters').show();
                        populate_cdrs_dep($(this).attr('id'));
                    });
                });
                break;
            case 'home':
                $('#sidebar').empty();
                $('#filters').hide();
                populate_cdrs();
                break;
            default:
                //error
        }
}

function populate_cdrs_emp(emp_id){
    $.getJSON('get_cdrs.php', {'emp': emp_id}, synth_cdr_table);
}
function populate_cdrs_dep(dep_id){
    
    $.getJSON('get_cdrs.php', {'dep': dep_id}, synth_cdr_table);
}

function populate_cdrs_ext(ext){
    //default to an empty string
    //ext = typeof ext !== 'undefined'? ext : '';
    
    $.getJSON('get_cdrs.php', {'ext': ext}, synth_cdr_table);
}

function populate_cdrs(){
    $.getJSON('get_cdrs.php', synth_cdr_table);
}

function synth_cdr_table(j_cdrs){
    var cdr_cols_captions = {
        'cdr_id': '',
        'ocg': 'Calling Party',
        'ocd': 'Called Party',
        'clock_org': 'Time Originated',
        'clock_con': 'Time Connected',
        'duration': 'Duration',
        'money': 'Cost (EGP)'};
    
    var cdr_tbl = '<tr>';
    for (col in cdr_cols_captions){
        cdr_tbl += '<th class="'+col+'">'+cdr_cols_captions[col]+'</th>';
    }
    cdr_tbl += '</tr>';


    //var i =6;
    $.each(j_cdrs, function(i, cdr){
        cdr_tbl += '<tr class="'+ cdr['dir']+'">';
        //alert(cdr['dir']);
        cdr_tbl += '<td class="cdr_id"><b>'+(i+1)+'</b></td>';
        for (col in cdr_cols_captions){
            if (col.search('clock')!=-1)
                cdr_tbl += '<td class="'+col+'">'+(Number(cdr[col])==0 ? '-' : format_date(new Date(Number(cdr[col])*1000)))+'</td>';
            else if (col == 'duration')
                cdr_tbl += '<td class="'+col+'">'+format_duration(cdr[col])+'</td>';
            else if (col == 'cdr_id')
                continue;
            else
                cdr_tbl += '<td class="'+col+'">'+cdr[col]+'</td>';
        }
        cdr_tbl += '</tr>';
    });
    $('#cdrs').html(cdr_tbl);
    $('#cdrs tr:odd').addClass('altcolor');
}
    
function format_date(date){
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    var h = date.getHours();
    var spec = (h>=12)? 'pm' : 'am';
    var mm = date.getMinutes();
    var s = date.getSeconds();
    
    m = (m<10)? '0'+m : m;
    d = (d<10)? '0'+d : d;
    h = (h>12)? h-12 : ((h<10)? '0'+h : h);
    mm = (mm<10)? '0'+mm : mm;
    s = (s<10)? '0'+s : s;
    
    
    return d+'/'+m+'/'+y+' '+h+':'+mm+':'+s+' '+spec;
    //return date.toDateString()+date.toTimeString();
    
}

function format_duration(sec){
    var hours = Math.floor(sec/(60*60));
    var mins = Math.floor((sec%(60*60))/60);
    var secs = sec-(hours*60*60)-(mins*60);
    
    hours = (hours<10)? '0'+hours : hours;
    mins = (mins<10)? '0'+mins : mins;
    secs = (secs<10)? '0'+secs : secs;
    
    
    return hours+':'+mins+':'+secs;
}

function nav_click_handle(btn_id){
    if (!$('#'+btn_id).hasClass('active')){
        $('nav a').removeClass('active');
        $('#'+btn_id).addClass('active');
        populate_sidebar(btn_id);
    }
}

function filter_cdr_table(btn_id){
    switch (btn_id){
        case "all_calls":
            $('#cdrs tr').show();
            break;
        case "in_calls":
            $('#cdrs tr.0, tr.2').hide();
            $('#cdrs tr.1').show();
            break;
        case "out_calls":
            $('#cdrs tr.1, tr.2').hide();
            $('#cdrs tr.0').show();
        default:
            //error
    }
}

function cdr_filter(btn_id){
    if (!$('#'+btn_id).hasClass('active')){
        $('#filters a').removeClass('active');
        $('#'+btn_id).addClass('active');
    }
    filter_cdr_table(btn_id);
}